<?php
/**
 *
 * User: crstu
 * Date: 2016/6/1
 * Time: 10:18
 */

namespace common\widgets;



use kartik\widgets\DatePicker;
use common\widgets\UploadImage;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use yii\widgets\InputWidget;

class FormTable extends Widget
{

    const INPUT_HIDDEN = 'hiddenInput';
    const INPUT_TEXT = 'textInput';
    const INPUT_TEXTAREA = 'textarea';
    const INPUT_PASSWORD = 'passwordInput';
    const INPUT_DROPDOWN_LIST = 'dropdownList';
    const INPUT_LIST_BOX = 'listBox';
    const INPUT_CHECKBOX = 'checkbox';
    const INPUT_RADIO = 'radio';
    const INPUT_CHECKBOX_LIST = 'checkboxList';
    const INPUT_RADIO_LIST = 'radioList';
    const INPUT_FILE = 'fileInput';
    const INPUT_HTML5 = 'input';
    const INPUT_WIDGET = 'widget';
    const INPUT_RAW = 'raw';
    const INPUT_EDITOR = 'editor';
    const INPUT_TREE_LIST = 'treeList';
    const INPUT_UPLOAD_IMAGE = 'uploadImage';
//    const INPUT_DATE_PICKER = 'datePicker';
//    const INPUT_TIME_PICKER = 'timePicker';
//    const INPUT_DATETIME_PICKER = 'dateTimePicker';


    public $labelWidth = 2;

    public $cols = 1;

    public $rows = [];

    public $form;

    public $model;


    public function init()
    {
        parent::init();
    }
    
    public function run()
    {
        parent::run();

        $model  = $this->model;
        $form   = $this->form;

        foreach ($this->rows as $key => $row){

            if($this->cols >= 2){
                $cols_width = 'col-sm-' . ceil(12/$this->cols);

                echo '<div class="row">';

                foreach ($row as $k => $r){
                    echo '<div class="'.$cols_width.'"><div class="row">';
                    echo $this->renderInput($model, $form, $k, $r);
                    echo '</div></div>';
                }
                if(count($row) === 1 )
                    echo '<div class="'.$cols_width.'">&nbsp;</div>';

                echo '</div>';

            }else{
                echo $this->renderInput($model, $form, $key, $row);
            }
        }
    }


    public function renderInput($model, $form, $attribute, $row){

        $type = $row['type'];
        $options = isset($row['options']) && is_array($row['options']) ? $row['options'] : [];

        $items     = isset($row['items']) ? $row['items'] : [];
        $width     = isset($row['width']) ? (int)$row['width'] : 4;
        $name      = Html::getInputName($model, $attribute);

        $fieldOptions = [];
        $fieldOptions['horizontalCssClasses']['label'] = 'col-sm-'.$this->labelWidth;
        $fieldOptions['horizontalCssClasses']['wrapper'] = 'col-sm-'.$width;

        $field = $form->field($model, $attribute, $fieldOptions);

        switch ($type) {
            case static::INPUT_HIDDEN:
            case static::INPUT_TEXT:
            case static::INPUT_TEXTAREA:
            case static::INPUT_PASSWORD:
            case static::INPUT_FILE:
                echo $field->$type($options);
                break;

            case static::INPUT_DROPDOWN_LIST:
                if(isset($options['initValue'])){
                    $items[0] = $options['initValue'];
                    ksort($items);
                }
                echo $field->$type($items, $options);
                break;

            case static::INPUT_LIST_BOX:
                echo $field->$type($items, $options);
                break;

            case static::INPUT_RADIO_LIST:
                $field->inline = 1;
                if(isset($options['defaultValue']) && is_null($this->model->$attribute))
                    $this->model->$attribute = $options['defaultValue'];
                Html::addCssClass($options, 'radio-list');
                echo $field->$type($items, $options);
                break;

            case static::INPUT_CHECKBOX_LIST:
                $field->inline = 1;
                if(isset($options['defaultValue']) && is_null($this->model->$attribute))
                    $this->model->$attribute = $options['defaultValue'];
                Html::addCssClass($options, 'checkbox-list');
                echo $field->$type($items, $options);
                break;

            case static::INPUT_EDITOR:
                echo self::editor($model, $attribute, $options);
                break;

            case static::INPUT_UPLOAD_IMAGE:
                echo self::upImage($model, $attribute, $options);
                break;

            case static::INPUT_TREE_LIST:
                $options['model'] = $model;
                $options['name'] = $name;
                $options['attribute'] = $attribute;
                echo SelectTree::widget($options);
                break;

            case static::INPUT_WIDGET:
                if(!isset($options['pluginOptions'])) $options['pluginOptions'] = [];
                $widgetClass = $this->getWidgetClass($row['widgetClass']);
                echo $field->$type($widgetClass, $options['pluginOptions']);
                break;

            default:
                echo '';
                break;
        }
    }


//    public static function listInput($model, $attribute, $type, $items, $options){
//
//        if($type == 'checkbox' || $type == 'checkboxList'){
//            $type = 'checkbox';
//        }elseif($type == 'radio' || $type == 'radioList'){
//            $type = 'radio';
//        }
//        $name  = Html::getInputName($model, $attribute);
//        $value = Html::getAttributeValue($model, $attribute);
//        $html = '<div class="'.$type.'-list">';
//        foreach ($items as $k => $v){
//            if(empty($value) && isset($options['default'])){
//                if($options['default'] == $k)
//                    $checked = 'checked';
//                else
//                    $checked = "";
//            }else{
//                $checked = ($value == $k) ? 'checked' : '';
//            }
//            $html .= '<label class="'.$type.'-inline">'
//                   .'<input type="'.$type.'" name="'.$name.'" value="'.$k.'" '.$checked.'/>'.$v.'</label>';
//        }
//        $html .= '</div>';
//        return $html;
//    }

    public static function editor($model, $attribute, $options){
        $options['model']     = $model;
        $options['attribute'] = $attribute;
        return Simditor::widget($options);
    }


    public static function upImage($model, $attribute, $options){
        $options['model'] = $model;
        $options['attribute'] = $attribute;
        return UploadImage::widget($options);
    }

    public static function datePicker($model, $attribute, $options){

    }

    /**
     * @param array $settings
     * @return mixed
     * @throws InvalidConfigException
     */
    protected function getWidgetClass($widgetClass)
    {
        if (empty($widgetClass) && !$widgetClass instanceof InputWidget) {
            throw new InvalidConfigException(
                "A valid 'widgetClass' must be setup and extend from '\\yii\\widgets\\InputWidget'."
            );
        }
        return $widgetClass;
    }

}